﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;
using WP.Controls.GridColumn;


using WP.RM.BusinessRule;
using WP.RM.BusinessLogical;
using WP.RM.DataAccess;

namespace WP.RM.Form
{
    public partial class frmRMWorkQtyInputQry : BaseQryRpt
    {
        private bool[] mDtlColumnReadOnlyArray;
        private DBAccess mdbcon = null;
        private string mstrCondition = "";
        public frmRMWorkQtyInputQry()
        {
            InitializeComponent();
            mdbcon = new DBAccess(false, false);
        }

        private enum Dtl
        {
            RowSeqNo=0,
            /// <summary>
            /// 车间序号
            /// </summary>
            colWorkShopSeqNo,
            /// <summary>
            /// 车间名称
            /// </summary>
            colWorkShopName,
            /// <summary>
            /// 日期
            /// </summary>
            colWorkDate,
            /// <summary>
            /// 工序编号
            /// </summary>
            colWorkingProcedureID,            
            /// <summary>
            /// 工序名称
            /// </summary>
            colWorkingProcedureName,

            /// <summary>
            /// 机台
            /// </summary>
            colMachine,
            /// <summary>
            /// 班别
            /// </summary>
            colWorkGroup,
            /// <summary>
            /// 员工编号
            /// </summary>
            colWorkerID,
            /// <summary>
            /// 姓名
            /// </summary>
            colWorkerName,
            /// <summary>
            /// 产品名称
            /// </summary>
            colProduct,
            /// <summary>
            /// 半成品
            /// </summary>
            colSemiCode,
            /// <summary>
            /// 半成品名称
            /// </summary>
            colSemiName,
            /// <summary>
            /// 工价类别
            /// </summary>
            colWorkCostType,
            /// <summary>
            /// 计件工价
            /// </summary>
            colWorkCost,
            /// <summary>
            /// 产量
            /// </summary>
            colQuantity,
            /// <summary>
            /// 总金额
            /// </summary>
            colTotal
        }

        private void InitGridDtl()
        {
            //colRowSeqNo,colWorkShopSeqNo,colWorkShopName
            this.dgvInputDtl.Columns.Clear();
            this.dgvInputDtl.Rows.Clear();

            DataGridViewTextBoxColumn colRowSeqNo = new DataGridViewTextBoxColumn();
            colRowSeqNo.HeaderText = "序号";
            colRowSeqNo.Visible = true;
            colRowSeqNo.Name = "RowSeqNo";
            colRowSeqNo.Width = 50;
            colRowSeqNo.ReadOnly = true;
            colRowSeqNo.SortMode = DataGridViewColumnSortMode.NotSortable;

            DataGridViewTextBoxColumn colWorkShopSeqNo = new DataGridViewTextBoxColumn();
            colWorkShopSeqNo.HeaderText = "车间序号";
            colWorkShopSeqNo.Width = 70;
            colWorkShopSeqNo.Name = "WorkShopSeqNo";
            colWorkShopSeqNo.ReadOnly = false;
            colWorkShopSeqNo.Visible = true;

            DataGridViewTextBoxColumn colWorkShopName = new DataGridViewTextBoxColumn();
            colWorkShopName.HeaderText = "车间序号";
            colWorkShopName.Width = 70;
            colWorkShopName.Name = "WorkShopName";
            colWorkShopName.ReadOnly = false;
            colWorkShopName.Visible = true;

            //colWorkDate,colWorkingProcedureID,colWorkingProcedureName,colMachine,colWorkGroup,
            DataGridViewTextBoxColumn colWorkDate = new DataGridViewTextBoxColumn();
            colWorkDate.HeaderText = "日期";
            colWorkDate.Width = 70;
            colWorkDate.Name = "WorkDate";
            colWorkDate.ReadOnly = false;
            colWorkDate.Visible = true;

            DataGridViewTextBoxColumn colWorkingProcedureID = new DataGridViewTextBoxColumn();
            colWorkingProcedureID.HeaderText = "工序编号";
            colWorkingProcedureID.Width = 70;
            colWorkingProcedureID.Name = "WorkingProcedureID";
            colWorkingProcedureID.ReadOnly = false;
            colWorkingProcedureID.Visible = true;

            DataGridViewTextBoxColumn colWorkingProcedureName = new DataGridViewTextBoxColumn();
            colWorkingProcedureName.HeaderText = "工序名称";
            colWorkingProcedureName.Width = 70;
            colWorkingProcedureName.Name = "WorkShopName";
            colWorkingProcedureName.ReadOnly = false;
            colWorkingProcedureName.Visible = true;

            DataGridViewTextBoxColumn colMachine = new DataGridViewTextBoxColumn();
            colMachine.HeaderText = "机台";
            colMachine.Width = 70;
            colMachine.Name = "MachineName";
            colMachine.ReadOnly = false;
            colMachine.Visible = true;

            DataGridViewTextBoxColumn colWorkGroup = new DataGridViewTextBoxColumn();
            colWorkGroup.HeaderText = "班组";
            colWorkGroup.Width = 70;
            colWorkGroup.Name = "WorkGroupName";
            colWorkGroup.ReadOnly = false;
            colWorkGroup.Visible = true;
            
            //colWorkerID,colWorkerName,colProduct,colSemiCode,colSemiName,
            DataGridViewTextBoxColumn colWorkerID = new DataGridViewTextBoxColumn();
            colWorkerID.HeaderText = "员工代码";
            colWorkerID.Width = 100;
            colWorkerID.Name = "WorkerID";
            colWorkerID.ReadOnly = true;
            colWorkerID.Visible = false;

            TextLookUpColumn colWorkerName = new TextLookUpColumn();
            colWorkerName.HeaderText = "员工姓名";
            colWorkerName.Width = 100;
            colWorkerName.Name = "WorkerName";
            colWorkerName.ReadOnly = false;
            colWorkerName.Visible = true;

            DataGridViewTextBoxColumn colProduct = new DataGridViewTextBoxColumn();
            colProduct.HeaderText = "产品名称";
            colProduct.Width = 100;
            colProduct.Name = "ProductName";
            colProduct.ReadOnly = false;
            colProduct.Visible = true;

            DataGridViewTextBoxColumn colSemiCode = new DataGridViewTextBoxColumn();
            colSemiCode.HeaderText = "半成品编码";
            colSemiCode.Width = 100;
            colSemiCode.Name = "SemiCode";
            colSemiCode.ReadOnly = false;
            colSemiCode.Visible = true;

            DataGridViewTextBoxColumn colSemiName = new DataGridViewTextBoxColumn();
            colSemiName.HeaderText = "半成品名称";
            colSemiName.Width = 100;
            colSemiName.Name = "SemiName";
            colSemiName.ReadOnly = false;
            colSemiName.Visible = true;

            //colWKPrictType,colUnitCost,colQuantity,colTotal
            DataGridViewTextBoxColumn colWKPrictType = new DataGridViewTextBoxColumn();
            colWKPrictType.HeaderText = "工价类别";
            colWKPrictType.Width = 100;
            colWKPrictType.Name = "WKPriceType";
            colWKPrictType.ReadOnly = false;
            colWKPrictType.Visible = true;

            //UnitCost,         //计件工价
            DataGridViewTextBoxColumn colUnitCost = new DataGridViewTextBoxColumn();
            colUnitCost.HeaderText = "计件工价";
            colUnitCost.Width = 100;
            colUnitCost.Name = "UnitCost";
            colUnitCost.ReadOnly = false;
            colUnitCost.Visible = true;
            colUnitCost.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            colUnitCost.HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomRight;

            //Qty,         //数量
            DataGridViewTextBoxColumn colQuantity = new DataGridViewTextBoxColumn();
            colQuantity.HeaderText = "产量";
            colQuantity.Width = 100;
            colQuantity.Name = "Qty";
            colQuantity.ReadOnly = false;
            colQuantity.Visible = true;
            colQuantity.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            colQuantity.HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomRight;

            DataGridViewTextBoxColumn colTotal = new DataGridViewTextBoxColumn();
            colTotal.HeaderText = "总金额";
            colTotal.Width = 100;
            colTotal.Name = "Qty";
            colTotal.ReadOnly = false;
            colTotal.Visible = true;
            colTotal.DefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomRight;
            colTotal.HeaderCell.Style.Alignment = DataGridViewContentAlignment.BottomRight;
            //Remark,         //备注
            DataGridViewTextBoxColumn colRemark = new DataGridViewTextBoxColumn();
            colRemark.HeaderText = "备注";
            colRemark.Width = 100;
            colRemark.Name = "Remark";
            colRemark.ReadOnly = false;
            colRemark.Visible = true;

            DataGridViewTextBoxColumn colRecID = new DataGridViewTextBoxColumn();
            colRecID.HeaderText = "RecID";
            colRecID.Visible = false;
            colRecID.Name = "RecID";

            //RecID  HdrRecID WorkerRecID WorkerID WorkerName NamePY WorkPositionSeqNo WKPriceType UnitCost Qty
            this.dgvInputDtl.Columns.AddRange(new DataGridViewColumn[] { colRowSeqNo,colWorkShopSeqNo,
            colWorkShopName,colWorkDate,colWorkingProcedureID,colWorkingProcedureName,colMachine,colWorkGroup,
            colWorkerID,colWorkerName,colProduct,colSemiCode,colSemiName,colWKPrictType,colUnitCost,colQuantity,colTotal
            });

            mDtlColumnReadOnlyArray = new bool[dgvInputDtl.Columns.Count];
            string[] ColumnWidthArray = DataFunctions.GetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.Name + "." + this.dgvInputDtl.Name).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < dgvInputDtl.Columns.Count; i++)
            {
                dgvInputDtl.Columns[i].ReadOnly = true;
                mDtlColumnReadOnlyArray[i] = dgvInputDtl.Columns[i].ReadOnly;
                if (ColumnWidthArray.Length == dgvInputDtl.Columns.Count) dgvInputDtl.Columns[i].Width = CvtFunc.ToInt(ColumnWidthArray[i]);
            }

            this.dgvInputDtl.EditMode = DataGridViewEditMode.EditOnEnter;
            this.dgvInputDtl.RowTemplate.Height = 20;
            this.dgvInputDtl.RowHeadersWidth = 23;
            this.dgvInputDtl.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
            this.dgvInputDtl.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            this.dgvInputDtl.ColumnHeadersHeight = 30;
            this.dgvInputDtl.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.dgvInputDtl.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.dgvInputDtl.AllowUserToResizeRows = false;
        }

        private void GetData()
        {
            rmWorkQtyInputQryDAL rmDAL = new rmWorkQtyInputQryDAL();
            rmWorkQtyInputQryDAL.RptPara rptPara = new rmWorkQtyInputQryDAL.RptPara();
            rptPara.dbcon = mdbcon;
            rptPara.strCondition = mstrCondition;

            rmDAL.GetData(rptPara);
        }

        private void FillGrid()
        {
            int intRow = 0;
            string strsQL = "SELECT * FROM #tmpDetail Order By WorkDate,WorkerID";
            DataTable dtDetail = mdbcon.GetDataTable(strsQL);
            foreach (DataRow drDtl in dtDetail.Rows)
            {
                this.dgvInputDtl.Rows.Add();
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.RowSeqNo].Value = (intRow + 1).ToString();
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colMachine].Value = CvtFunc.ToString(drDtl["MachineName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colProduct].Value = CvtFunc.ToString(drDtl["ProductName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colQuantity].Value = CvtFunc.ToString(drDtl["Qty"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colSemiCode].Value = CvtFunc.ToString(drDtl["SemiCode"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colSemiName].Value = CvtFunc.ToString(drDtl["SemiName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colTotal].Value = CvtFunc.ToString(drDtl["Total"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkCost].Value = CvtFunc.ToString(drDtl["UnitCost"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkCostType].Value = CvtFunc.ToString(drDtl["WKPriceType"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkDate].Value = CvtFunc.ToString(drDtl["WorkDate"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkerID].Value = CvtFunc.ToString(drDtl["WorkerID"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkerName].Value = CvtFunc.ToString(drDtl["WorkerName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkGroup].Value = CvtFunc.ToString(drDtl["WorkGroupName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkingProcedureID].Value = CvtFunc.ToString(drDtl["WorkingProcedureID"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkingProcedureName].Value = CvtFunc.ToString(drDtl["WorkingProcedureName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkShopName].Value = CvtFunc.ToString(drDtl["WorkShopName"]);
                this.dgvInputDtl.Rows[intRow].Cells[(int)Dtl.colWorkShopSeqNo].Value = CvtFunc.ToString(drDtl["WorkShopSeqNo"]);              
            }
            
            this.dgvInputDtl.EndEdit();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if ( mstrCondition =="")
            {
                MsgBoxEx.Show("请选择查询条件","",  MessageBoxButtons.OK, MessageBoxIcon.Exclamation );  
                return ;
            }

            GetData();
            FillGrid();
        }

        private void btnCondition_Click(object sender, EventArgs e)
        {
            frmRMWorkQrySetting frmSetting = new frmRMWorkQrySetting(frmRMWorkQrySetting.FromSettingType.考勤录入查询);
            string strCondition = frmSetting.GetCondition();
            if (strCondition != "")
            {
                mstrCondition = strCondition;
            }
        }

        private void btnToExcel_Click(object sender, EventArgs e)
        {
            ToExcel.DataGirdViewToExcel(this.dgvInputDtl);  
        }

        private void frmRMWorkQtyInputQry_Load(object sender, EventArgs e)
        {
            InitGridDtl();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp(); 
        }

        private void frmRMWorkQtyInputQry_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataFunctions.SetGridColumnWidthString(this.Name, this.CurrentMenuInfo.iParameter, SysVar.UserName, this.dgvInputDtl);
        }

    }
}
